<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <!-- <a-button type="primary" @click="handleForm({})" v-auth="'device:fault:faultManagement:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button> -->
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ tid: record.tid })">
          {{ record.subjectId }}
        </a>
      </template>
      <!-- 新增 actionColumn 插槽 -->
      <template #actionColumn="{ record }">
        <a-button @click="handleForm({ tid: record.tid })"  type="text">
          <span style="font-size: 14px; color: #4169E1;">{{ t('详情') }}</span>
        </a-button>
        <!-- <Popconfirm
          title="是否确认删除该数据"
          ok-text="确认"
          cancel-text="取消"
          @confirm="handleDelete(record)"
        >
          <a-button v-auth="'device:fault:faultManagement:edit'" type="text">
            <span style="font-size: 14px; color: red;">{{ t('删除') }}</span>
          </a-button>
        </Popconfirm> -->
        <a-button @click="handleTrace(record)"  type="text" v-if="record.status != '9'">
          <span style="font-size: 14px; color:#FFC125;">{{ t('流程追踪') }}</span>
        </a-button>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <BpmRuntimeTrace @register="registerTraceModal" />
  </div>
</template>
<script lang="ts" setup name="ViewsDeviceFaultFaultManagementList">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { faultManagementDelete, faultManagementListData } from '/@/api/device.fault/faultManagement';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { BpmRuntimeTrace } from '/@/components/Bpm';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { Popconfirm } from 'ant-design-vue';

  const { t } = useI18n('device.fault.faultManagement');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('fault_management管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { md: 8, lg: 6 },
    labelWidth: 90,
    schemas: [
      // {
      //   label: t('故障发生主体的ID'),
      //   field: 'subjectId',
      //   component: 'Input',
      // },
      {
        label: t('所属主体'),
        field: 'subjectName',
        component: 'Input',
      },
      // {
      //   label: t('区域ID'),
      //   field: 'regionId',
      //   component: 'Input',
      // },
      {
        label: t('区域名称'),
        field: 'regionName',
        component: 'Input',
      },
      // {
      //   label: t('建筑ID'),
      //   field: 'buildingId',
      //   component: 'Input',
      // },
      {
        label: t('建筑名称'),
        field: 'buildingName',
        component: 'Input',
      },
      {
        label: t('故障类型'),
        field: 'faultType',
        component: 'Input',
      },
      // {
      //   label: t('设备ID'),
      //   field: 'deviceId',
      //   component: 'Input',
      // },
      {
        label: t('设备名称'),
        field: 'deviceName',
        component: 'Input',
      },
      // {
      //   label: t('所属主机ID'),
      //   field: 'hostId',
      //   component: 'Input',
      // },
      {
        label: t('所属主机名称'),
        field: 'hostName',
        component: 'Input',
      },
      {
        label: t('是否恢复'),
        field: 'isRecovered',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('所属主体'),
      dataIndex:'subjectName',
      key: 'a.subject_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('区域名称'),
      dataIndex:'regionName',
      key: 'a.region_name',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('建筑名称'),
      dataIndex: 'buildingName',
      key: 'a.building_name',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('故障类型'),
      dataIndex: 'faultType',
      key: 'a.fault_type',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('设备名称'),
      dataIndex: 'deviceName',
      key: 'a.device_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('设备编号'),
      dataIndex: 'deviceId',
      key: 'a.device_id',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('所属主机名称'),
      dataIndex: 'hostName',
      key: 'a.host_name',
      sorter: true,
      width: 110,
      align: 'left',
    },
    {
      title: t('所属主机编号'),
      dataIndex: 'hostId',
      key: 'a.host_id',
      sorter: true,
      width: 100,
      align: 'left',
    },
    // {
    //   title: t('恢复正常图片的ID'),
    //   dataIndex:'recoveryImageId',
    //   key: 'a.recovery_image_id',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    // {
    //   title: t('恢复正常图片的URL'),
    //   dataIndex:'recoveryImageUrl',
    //   key: 'a.recovery_image_url',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    {
      title: t('处理人'),
      dataIndex: 'handlerName',
      key: 'a.handler_name',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('处理人联系方式'),
      dataIndex: 'handlerContact',
      key: 'a.handler_contact',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('处理方式'),
      dataIndex: 'handlingMethod',
      key: 'a.handling_method',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
    title: t('操作'),
    dataIndex: 'action',
    key: 'action',
    width: 250,
    fixed: 'right',
    slot: 'actionColumn' // 指定使用actionColumn插槽
  }
  ];

  // 移除原有的 actionColumn，因为现在使用插槽来定义操作按钮
  // const actionColumn: BasicColumn = {
  //   width: 160,
  //   actions: (record: Recordable) => [
  //     {
  //       icon: 'i-ant-design:copy-twotone',
  //       title: t('设备故障详情'),
  //       onClick: handleForm.bind(this, { tid: record.tid }),
  //       auth: 'device:fault:faultManagement:edit',
  //     },
  //     {
  //       icon: 'i-ant-design:delete-outlined',
  //       color: 'error',
  //       title: t('删除fault_management'),
  //       popConfirm: {
  //         title: t('是否确认删除fault_management'),
  //         confirm: handleDelete.bind(this, record),
  //       },
  //       auth: 'device:fault:faultManagement:edit',
  //       ifShow: () => record.status == '9',
  //     },
  //     {
  //       icon: 'i-fluent:flowchart-20-regular',
  //       title: t('流程追踪'),
  //       onClick: handleTrace.bind(this, record),
  //       ifShow: () => record.status != '9',
  //     },
  //   ],
  // };

  const [registerTable, { reload }] = useTable({
    api: faultManagementListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    // actionColumn: actionColumn, // 移除这一行
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  const [registerDrawer, { openDrawer }] = useDrawer();

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { tid: record.tid };
    const res = await faultManagementDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  const [registerTraceModal, { openModal: traceModel }] = useModal();

  function handleTrace(record: Recordable) {
    traceModel(true, { formKey: 'Equipment_Failure_Process', bizKey: record.id });
  }
</script>